using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Button:Base
    {
        private DAL.Button dal = null;
        public Button()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.Button(isession);
        }

        public Common.Result Add(Model.Button value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            if (string.IsNullOrEmpty(value.WebSiteCode) || new BLL.WebSite().GetModel(value.WebSiteCode) == null)
            {
                ret.message = "[站点]不存在";
                return ret;
            }
            if (string.IsNullOrEmpty(value.MenuCode) || new BLL.Menu().GetModel(value.WebSiteCode,value.MenuCode) == null)
            {
                ret.message = "[上级菜单]不存在";
                return ret;
            }
            if (dal.GetModel(value.WebSiteCode, value.MenuCode, value.Code) != null)
            {
                ret.message = "[权限编号]已经存在";
                return ret;
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.Button value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.WebSiteCode,value.MenuCode,value.Code);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在,无法修改";
                return ret;
            }
            dalvalue.Name = value.Name;
            ret = dal.Update(dalvalue);
            return ret;
        }


        public Common.Result Delete(string webSiteCode, string MenuCode, string ButtonCode)
        {
            var ret = dal.Delete(webSiteCode, MenuCode, ButtonCode);
            return ret;
        }

        public Model.Button GetModel(string WebSiteCode, string MenuCode, string ButtonCode)
        {
            return dal.GetModel(WebSiteCode, MenuCode, ButtonCode);
        }

        public IList<Model.Button> GetByParent(string webSiteCode, string menuCode)
        {
            return dal.GetByParent(webSiteCode, menuCode);
        }

        public IList<Model.Button> Get(string where, string orderby, int start, int length,params object[] ps)
        {
            return dal.Get(where, orderby, start, length,ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

    }
}
